<template>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="消耗品id" prop="id">
            <el-input v-model="ruleForm.id"></el-input>
        </el-form-item>
        <el-form-item label="消耗品名称" prop="name">
            <el-input v-model="ruleForm.goodsName" ></el-input>
        </el-form-item>
        <el-form-item label="消耗品类型" prop="type">
            <el-input v-model="ruleForm.goodsClassifyName"></el-input>
        </el-form-item>
        <el-form-item label="消耗品价格" prop="price">
            <el-input v-model="ruleForm.unit"></el-input>
        </el-form-item>
        <el-form-item label="消耗品数量" prop="price">
            <el-input v-model="ruleForm.stock"></el-input>
        </el-form-item>
        <el-form-item label="供应商" prop="price">
            <el-input v-model="ruleForm.provider"></el-input>
        </el-form-item>


        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">确认添加</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
    </el-form>
</template>
<script>
    import {mapActions, mapState} from 'vuex'
    export default {
        data() {
            return {
                ruleForm: {
                    id: '',
                    goodsName: '',
                    goodsClassifyName: '',
                    unit: '',
                    stock:'',
                    provider:'',
                },
                rules: {
                    id: [
                        { required: true, message: '请输入消耗品id', trigger: 'blur' },
                    ],
                    goodsName: [
                        { required: true, message: '请输入消耗品名称', trigger: 'blur' },
                        { min: 2, max: 10, message: '长度在 2到 10个字符', trigger: 'blur' }
                    ],
                    goodsClassifyName: [
                        { required: true, message: '请输入消耗品类型', trigger: 'blur' },
                        { min: 2, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ],
                    unit: [
                        { required: true, message: '请输入消耗品价格', trigger: 'blur' },
                    ],
                    stock: [
                        { required: true, message: '请输入消耗品数量', trigger: 'blur' },
                    ],
                    provider: [
                        { required: true, message: '请输入消耗品供应商', trigger: 'blur' },
                    ],
                }
            };
        },
        methods: {
            ...mapActions(['addxhp']),
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                       /* console.log(this.ruleForm)*/
                        let data={
                            'id': this.ruleForm.id,
                            "goodsClassifyName": this.ruleForm.goodsClassifyName,
                            "goodsName": this.ruleForm.goodsName,
                            "provider": this.ruleForm.provider,
                            "stock": this.ruleForm.stock,
                            "unit": this.ruleForm.unit,
                            "goodsUnitsName": 0,
                            "saturation": 500,
                        }
                        this.addxhp(data)
                        this.$message({
                            type: 'success',
                            message: '添加成功!'
                        })
                    } else {
                       /* console.log('error submit!!');*/
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>